import { defineStore } from 'pinia'
import request from "../utils/request"
import { encrypt } from "../views/home/system/managers/encrypt"
import { ElMessage } from 'element-plus'
const managers = defineStore('managers', {
    state: () => {
        return {
            managers_list: [],//管理员页面数据列表
            flag_list: [],//管理员页面弹框中的角色数据
            list_Len: 0,
            filter_list: [],
        }
    },
    actions: {
        managers_getlist() {
            request({
                url: "/sys/user/page?t=1669100849218&current=1&size=100",
                method: "get",
            }).then(res => {
                console.log(res, "res长度");

                this.managers_list = res.data.records
                this.filter_list = res.data.records
                this.list_Len = res.data.total
            })
        },
        managers_getflaglist() {
            request({
                url: `/sys/role/list`,
                method: "get"
            }).then(res => {
                console.log(res, "res");

                this.flag_list = res.data
            })
        },
        add_list(obj: any) {
            console.log(obj, "obj");
            request({
                url: `/sys/user?t=${new Date().getTime()}`,
                method: "post",
                data: {
                    username: obj.name,
                    email: obj.email,
                    mobile: obj.phone,
                    password: encrypt(obj.pass),
                    roleIdList: obj.type,
                    status: obj.resource == "正常" ? 1 : 0
                }
            }).then(res => {
                console.log(res, "res");
                if (res.status == 200) {
                    ElMessage({
                        message: '添加成功成功',
                        type: 'success',
                    })
                }
                this.managers_getlist()
            })
        },
        edit_list(obj: any) {
            console.log(obj, "123");

            request({
                url: `/sys/user`,
                method: "put",
                data: {
                    username: obj.name,
                    email: obj.email,
                    mobile: obj.phone,
                    t: new Date().getTime(),
                    password: encrypt(obj.pass),
                    roleIdList: obj.type,
                    status: obj.resource == "正常" ? 1 : 0,
                    userId: obj.userId
                }
            }).then(res => {
                if (res.status == 200) {
                    ElMessage({
                        message: '编辑成功',
                        type: 'success',
                    })
                    this.managers_getlist()
                }

            })

        },
        search_list(username: String) {
            request({
                url: `/sys/user/page?t=1669347774158&current=1&size=10&username=${username}`,
                method: "get",
            }).then(res => {
                console.log(res, "res");
                this.filter_list = res.data.records
                // this.managers_getlist()
                console.log(this.filter_list, "this.filter_list");

            })
        },
        del_list(id: number) {
            request({
                url: `/sys/user`,
                method: "delete",
                data: [id]
            }).then(res => {
                console.log(res, "delete");
                if (res.status == 200) {
                    ElMessage({
                        type: "success",
                        message: "删除成功",
                    });
                }
                this.managers_getlist()
            })
        },

    },

})

export default managers